package be.donovan.gui.helper.components;

import java.awt.Component;
import java.util.Date;

import javax.swing.AbstractCellEditor;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;

import org.jdesktop.swingx.JXDatePicker;
import org.joda.time.YearMonthDay;

public class DatePickerTableCellEditor extends AbstractCellEditor implements TableCellEditor{

    private JXDatePicker datePicker;
    
    public DatePickerTableCellEditor() {
        datePicker = new JXDatePicker();
    }
    
    public DatePickerTableCellEditor(JXDatePicker picker) {
        datePicker = picker;
    }
    
    public Object getCellEditorValue() {
        return new YearMonthDay(datePicker.getDate().getTime());
    }

    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        if (value == null) {
            datePicker.setDate(new Date());
        } else if (value instanceof YearMonthDay) {
            datePicker.setDateInMillis(((YearMonthDay)value).toDateMidnight().getMillis());
        } else {
            throw new IllegalArgumentException("The DateCellEditor cannot be used with non Date types");
        }
        return datePicker;
    }


}
